#!/usr/bin/python3
# -*- coding:utf-8 -*-
#python version: 3.6.7

import requests

def get_post_html(url, headers, cookies, data = None, refile = False):
    #这个循环是尝试访问网址
    i = 0
    while i < 7:
        #写个try，脚本报异常也不会中止
        try:
            #如果没有data就get网页，否则post网页
            if data == None:
                #get取到网页源码
                html = requests.get(
                    url,
                    headers = headers,
                    cookies = cookies,
                    timeout = 15
                )
                html = html.content if refile else html.text
            else:
                #post取到网页源码
                html = requests.post(
                    url,
                    headers = headers,
                    cookies = cookies,
                    data = data,
                    timeout = 15
                )
                html = html.content if refile else html.text
            #只要访问成功就跳出访问的循环
            break
        except requests.exceptions.RequestException:
            i += 1
        #如果访问出现异常，就返回异常信息文本
        except Exception as e:
            error_n = 'Error!!!'.center(72, '*') + '\n'
            error_n += repr(e)
            error_n += '\n' + 'Error!!!'.center(72, '*')
            return error_n
    #访问正常则返回html代码
    return html

